﻿Imports System.Xml
Imports System.Globalization
Imports System.Text

Public Class PromotionEngine
    Public Function CallPromotion(ByVal XMLData As String) As DataSet
        Dim dsSourceData As DataSet
        Dim dsPromotionReturn As DataSet

        dsSourceData = ConvertXMLString2Dataset(XMLData)

        '------------------------LOGIC TO CALCULATE PROMOTION----------------------


        '--------------------------------------------------------------------------

        Return dsPromotionReturn

    End Function
    Public Function ConvertXMLString2Dataset(ByVal xmlString As String, Optional ByVal multipleXML As Boolean = False, Optional ByVal ObjType As String = "") As DataSet
        Dim dsBO As New DataSet()
        Dim xml As New XmlDocument()
        xml.LoadXml(xmlString)
        BuildDataSet(dsBO, xml.ChildNodes, multipleXML, ObjType)
        Return dsBO
    End Function
    Private Sub BuildDataSet(ByVal ds As DataSet, ByVal parentNode As XmlNodeList, ByVal multipleXML As Boolean, ByVal ObjType As String)
        For Each nodeParent As XmlNode In parentNode
            If nodeParent.Name.Equals("BO") Or (ObjType = "25" And nodeParent.Name.Equals("Deposit")) Then
                For Each node As XmlNode In nodeParent.ChildNodes
                    If node.Name.Equals("AdmInfo") Then
                        Continue For
                    End If
                    BuildTable(ds, node)
                Next
                If Not multipleXML Then
                    Exit For
                End If
            Else
                BuildDataSet(ds, nodeParent.ChildNodes, multipleXML, ObjType)
            End If
        Next
    End Sub
    Private Sub BuildTable(ByVal ads As DataSet, ByVal nodeTable As XmlNode)
        Dim dt As New DataTable(nodeTable.Name)
        Dim firstNode As XmlNode = nodeTable.FirstChild
        If Not IsNothing(firstNode) Then
            For Each col As XmlNode In firstNode.ChildNodes
                If Not dt.Columns.Contains(col.Name) Then
                    dt.Columns.Add(New DataColumn(col.Name, Type.[GetType]("System.String")))
                End If
            Next
        End If

        For Each rowNode As XmlNode In nodeTable.ChildNodes
            Dim dr As DataRow = dt.NewRow()
            For Each colValue As XmlNode In rowNode.ChildNodes
                If Not dt.Columns.Contains(colValue.Name) Then
                    dt.Columns.Add(colValue.Name)
                End If
                dr(colValue.Name) = colValue.InnerText
            Next
            dt.Rows.Add(dr)
        Next
        If Not ads.Tables.Contains(dt.TableName) Then
            ads.Tables.Add(dt.Copy)
        End If

    End Sub
    Public Function ConvertDataset2XMLString(ByVal ObjType As String, ByVal ds As DataSet, Optional ByVal GetFieldEmpty As Boolean = False) As String
        Try
            'Dim gf As New GeneralFunctions()
            Dim XmlString As New StringBuilder()
            Dim writer As XmlWriter = XmlWriter.Create(XmlString)
            writer.WriteStartDocument()
            If True Then
                writer.WriteStartElement("BOM")
                If True Then
                    writer.WriteStartElement("BO")
                    If True Then
                        '#Region "write ADMINFO_ELEMENT"
                        writer.WriteStartElement("AdmInfo")
                        If True Then
                            writer.WriteStartElement("Object")
                            If True Then
                                writer.WriteString(ObjType)
                            End If
                            writer.WriteEndElement()
                        End If
                        writer.WriteEndElement()
                        '#End Region

                        '#Region "Header&Line XML"
                        For Each dt As DataTable In ds.Tables
                            If dt.Rows.Count > 0 Then
                                writer.WriteStartElement(dt.TableName.ToString(CultureInfo.InvariantCulture))
                                If True Then
                                    For Each row As DataRow In dt.Rows
                                        writer.WriteStartElement("row")
                                        If True Then
                                            For Each column As DataColumn In dt.Columns
                                                If column.DefaultValue.ToString() <> "xx_remove_xx" Then
                                                    'Force datetime format follow SQL 
                                                    If column.DataType Is GetType(DateTime) And Not IsDBNull(row(column)) Then
                                                        Dim dateTime As DateTime
                                                        dateTime = row(column)
                                                        If GetFieldEmpty Then
                                                            writer.WriteStartElement(column.ColumnName)
                                                            writer.WriteString(IIf(IsDBNull(dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)), "", dateTime.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)))
                                                            writer.WriteEndElement()
                                                        Else
                                                            If row(column).ToString() <> "" Then
                                                                writer.WriteStartElement(column.ColumnName)
                                                                writer.WriteString(row(column).ToString)
                                                                writer.WriteEndElement()
                                                            End If
                                                        End If
                                                    ElseIf column.DataType Is GetType(Date) And Not IsDBNull(row(column)) Then
                                                        Dim d As Date
                                                        d = row(column)

                                                        If GetFieldEmpty Then
                                                            writer.WriteStartElement(column.ColumnName)
                                                            writer.WriteString(IIf(IsDBNull(d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)), "", d.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)))
                                                            writer.WriteEndElement()
                                                        Else
                                                            If row(column).ToString() <> "" Then
                                                                writer.WriteStartElement(column.ColumnName)
                                                                writer.WriteString(row(column).ToString)
                                                                writer.WriteEndElement()
                                                            End If
                                                        End If
                                                    Else
                                                        'Write Tag
                                                        If GetFieldEmpty Then
                                                            writer.WriteStartElement(column.ColumnName)
                                                            writer.WriteString(IIf(IsDBNull(row(column)), "", row(column)))
                                                            writer.WriteEndElement()
                                                        Else
                                                            If row(column).ToString() <> "" Then
                                                                writer.WriteStartElement(column.ColumnName)
                                                                writer.WriteString(row(column).ToString)
                                                                writer.WriteEndElement()
                                                            End If
                                                        End If
                                                    End If
                                                End If
                                            Next
                                        End If
                                        writer.WriteEndElement()
                                    Next
                                End If
                                writer.WriteEndElement()
                            End If
                        Next
                        '#End Region
                    End If
                    writer.WriteEndElement()
                End If
                writer.WriteEndElement()
            End If
            writer.WriteEndDocument()

            writer.Flush()

            Return XmlString.ToString()
        Catch ex As Exception
            Return ex.ToString()
        End Try
    End Function
End Class
